package com.scu.servlet;

import com.alibaba.fastjson.JSON;
import com.scu.dao.GoodDao;
import com.scu.entity.Good;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/findGoodServlet")

public class findGoodServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String goodname = request.getParameter("goodname");
        GoodDao goodDao = new GoodDao();
        try {
            List<Good> list = goodDao.findGood(goodname);
            //System.out.println(list);
            String listStr = JSON.toJSONString(list);

            //System.out.println(listStr);
            PrintWriter out = response.getWriter();
            out.print(listStr);
            out.flush();
            out.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String goodname = request.getParameter("goodname");
        GoodDao goodDao = new GoodDao();
        try {
            List<Good> list = goodDao.findGood(goodname);
            //System.out.println(list);
            String listStr = JSON.toJSONString(list);

            //System.out.println(listStr);
            PrintWriter out = response.getWriter();
            out.print(listStr);
            out.flush();
            out.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}